const app = getApp();
var api = require('../../../api.js');
var mta = require('../../../mta_analysis.js');

Page({
  data: {
    user:{},
    // notice: "",
    backgroundImg: "../../../images/tiaodai.png",
    info:[],
    truename: '',
    mobile: '',
    disabled: false, //避免重复提现
  },
  onLoad: function (options) {
    // console.log(options);
    mta.Page.init();
    app.bind();
    var that = this;
    var userInfo = wx.getStorageSync('user_info');
    var name = userInfo.nickName;
    var img = userInfo.avatarUrl;
    var userAll = that.data.user;
    userAll.userName = name;
    userAll.userImg = img;
    that.setData({
      user: userAll
    })
    // this.getUserInfo();
    // app.judge();

    wx.getNetworkType({
      success: function (res) {
        console.log(res)
        that.setData({
          networkType: res.networkType
        })
      }
    })
    
  },
  onReady: function () {

  },
  onShow: function () {
  
    this.getUserInfo();
  },
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {
    
  },
  onPullDownRefresh: function () {  //下拉刷新

    app.bind();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();
    
  },
  onReachBottom: function () {  //上拉加载
    
  },
  onShareAppMessage: function (e) {
    

    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };  
    // wx.hideShareMenu()
  },
  // 个人中心的信息
  getUserInfo: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url:api.user.userInfo,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
      },
      success: function (e) {
        console.log(e);
        var list = e.data.data;
        console.log(list);
        if (e.data.code == 0) {
          that.setData({
            info: e.data.data
          })
        } else if (e.data.code == -1){
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        } else if (e.data.code == 1){
          wx.navigateTo({
            url: '/pages/login/login'
          })
        }
      }
    });
  },
  // 客服
  toKefu: function(e) {
    console.log(e);
  },
  // 会员升级
  toUpgrande: function(e) {
    // wx.navigateTo({
    wx.switchTab({
      url: '/pages/user/upgrade/upgrade'
    })
  },
  // 个人信息
  toInformation:function(){
    wx.navigateTo({
      url: '/pages/user/information/information'
    })
  },
  //解绑邀请人
  relieve: function (e) {
    console.log("解绑邀请人");
    console.log(e);
    var that = this;
    var access_token = wx.getStorageSync('access_token');

    wx.showModal({ //提示1
      title: '解绑提示',
      content: '解绑后，邀请人将无法从你在平台的消费获得奖励，是否确定解绑？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定');

          wx.request({ //解绑
            url: api.user.break,
            method: "post",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              access_token: access_token,
            },
            success: function (e) {
              console.log(e);

              // wx.setStorageSync("jb", 'ok');
              wx.setStorageSync("parent", '无人介绍');
              
              that.data.info.user_info.parent = '无人介绍';

              that.setData({
                info: that.data.info
              })
          
              var atitle = e.data.data;

              wx.showModal({  //提示2
                title: atitle,
                content: '你可以通过邀请人的分享链接或海报进入平台，并进行绑定！',
                showCancel: false,
                success: function (e) {
                  console.log("用户点击确定");
                }
              })

            }
            
          });



        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 提示
  toNotice: function(e) {
    console.log("提示");
    console.log(e);
  },
  // 道财说明
  idealExplain: function (e) {
    console.log("道财说明");
    console.log(e);
    wx.navigateTo({
      url: '/pages/user/article/article',
    })
  },
  // 提现全部
  bringUpAll: function (e) {
    var that = this;
    wx.showModal({ //提示
      title: '提示',
      content: '平台代扣10%个人所得税，实际到账为提现金额的90%，请知悉！',
      success: function(res) {
        if (res.confirm) {

          that.setData({
            disabled: true,
          })

          var access_token = wx.getStorageSync('access_token');

          wx.request({ //提现申请
            url:api.user.apply,
            method: "post",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              access_token: access_token,
            },
            success: function (e) {
              console.log(e);
              var title = e.data.data;
              if (e.data.code == 0) {
                // wx.showToast({
                //   title: title,
                //   icon: 'none',
                // });
                wx.navigateTo({
                  url: '/pages/user/puForward/puForward'
                })
              } else {
                wx.showToast({
                  title: title,
                  icon: 'none',
                });
              }
              that.getUserInfo();
            },
            complete: function (e) {
              that.setData({
                disabled: false,
              })
            }
          });
         
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },  
  toMyCard:function(){
    wx.navigateTo({
      url: '/pages/user/card/card/card'
    })
  },
  // 好友
  toMyFriend:function(){
    wx.navigateTo({
      url: '/pages/user/myFriend/myFriend'
    })
  },
  // 我的奖励 —— 奖励明细
  toRewardDetails:function(){
    wx.navigateTo({
      url: '/pages/user/rewardDetails/rewardDetails'
    })
  },
  // 我的奖励 —— 提现记录
  toForWard: function () {
    wx.navigateTo({
      url: '/pages/user/forward/forward'
    })
  },
  // 我的奖励 —— 每月奖励金额
  toRewardMoney: function () {
    wx.navigateTo({
      url: '/pages/user/rewardMoney/rewardMoney'
    })
  },
  // getinfo: function (e) {
  //   var that = this;
  //   var access_token = wx.getStorageSync('access_token');
  //   wx.request({
  //     url:api.user.info,
  //     method: "get",
  //     header: {
  //       'content-type': 'application/x-www-form-urlencoded'
  //     },
  //     data: {
  //       access_token: access_token,
  //     },
  //     success: function (e) {
  //       var list = e.data.data;
  //       if (e.data.code == 0) {
  //         that.setData({
  //           truename: e.data.data.truename,
  //           mobile: e.data.data.mobile
  //         })
  //       } else {
  //         wx.navigateTo({
  //           url: '/pages/grant/grant'
  //         })
  //       }
  //     }
  //   });
  // },
})